package Data_Structure.Tree;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @className: BinaryTree
 * @author: 芃
 * @description: 二叉树->1
 * @date: 2023/8/17 19:52
 */
public class BinaryTree {
    public TreeNode root;//根节点

    //创建二叉树
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //D.left = H;
        E.right = H;
        return A;
    }

    //※二叉树的遍历：前序、中序、后序、层序
    //前序遍历 根左右
    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历 左根右
    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val);
        inOrder(root.right);
    }
    //后序遍历 左右根
    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val);
    }

    //层序遍历 根据层遍历-》借助队列
    public void levelOrder(TreeNode root){
        if (root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
    }
    //获取节点个数->子问题思路
    public int size(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftSize = size(root.left);
        int rightSize = size(root.right);
        return leftSize + rightSize + 1;
    }

    //获取叶子节点个数->子问题思路
    public int getLeafNodeCount(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        int left = getLeafNodeCount(root.left);
        int right = getLeafNodeCount(root.right);
        return left + right;
    }

    //第K层节点的个数->子问题思路
    public int getKLevelNodeCount(TreeNode root,int k){
        if (root == null || k <= 0){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        int newK = --k;
        int left = getKLevelNodeCount(root.left,newK);
        int right = getKLevelNodeCount(root.right,newK);
        return left + right;
    }

    //获取二叉树的高度
    //时间复杂度：O(n)
    //空间复杂度：O(log N)
    public int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

    //检测值为value的元素是否存在
    public boolean existNode(TreeNode root,char val){
        if (root == null){
            return false;
        }
        if (root.val == val){
            return true;
        }
        boolean leftExist = existNode(root.left,val);
        boolean rightExist = existNode(root.right,val);
        return leftExist || rightExist;
    }

    //判断一棵树是不是完全二叉树->借助队列
    public boolean isCompleteTree(TreeNode root){
        if (root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty() && queue.peek() != null){
            TreeNode top = queue.poll();
            if (top != null){
                queue.offer(top.left);
                queue.offer(top.right);
            }
        }
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                return false;
            }
        }
        return true;
    }

}
